package com.hx.haowumall.haowumallcart.controller;

import com.hx.common.constant.AuthServerConstant;
import com.hx.haowumall.haowumallcart.interceptor.CartInterceptor;
import com.hx.haowumall.haowumallcart.service.CartService;
import com.hx.haowumall.haowumallcart.vo.CartItemVo;
import com.hx.haowumall.haowumallcart.vo.CartVo;
import com.hx.haowumall.haowumallcart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVo> getCurrentUserCartItems(){

        return cartService.getUserCartItems();
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.haowumall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){
        cartService.changeCountItem(skuId,num);
        return "redirect:http://cart.haowumall.com/cart.html";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check){
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.haowumall.com/cart.html";
    }

    /**
     * 浏览器有一个cookie：user-key：标识用户身份，一个月过期时间
     * 如果第一次使用购物车功能，给一个临时身份
     * 浏览器保存临时身份，每次访问都会带上cookie
     *
     * 登录：查看session
     * 未登录：查看cookie里的user—key
     * 第一次使用创建一个user-key
     * */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        //1、快速得到用户信息 解决方法：ThreadLocal—同一个线程共享数据（原理：map<Thread,object>ThreadLocal）
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoTo);

        CartVo cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * RedirectAttributes ra
     *  ra.addFLashAttribute()；将数据放在session里面可以在页面取出，但是只能取一次（模拟session）
     *  ra.addAttribute("skuId", skuId)；将数据放在url后面
     * 添加商品到购物车
     * */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);
        return "redirect:http://cart.haowumall.com/addToCartSuccess.html";
    }

    /**
     * 跳转到成功页
     * */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
         CartItemVo item = cartService.getCartItem(skuId);
         model.addAttribute("item",item);
        return "success";
    }
}
